package io.xxx.omni.osc.sync

import com.alibaba.fastjson.JSONObject
import io.xxx.omni.osc.common.dateTimeFormatter
import io.xxx.omni.osc.common.toJSONObject
import io.xxx.omni.osc.common.toLocalDateTime
import io.xxx.omni.osc.domain.Document
import io.xxx.omni.osc.domain.DocumentType
import io.xxx.omni.osc.domain.PlatformIdentity
import io.xxx.omni.osc.sdk.WmClient
import org.springframework.stereotype.Component
import java.time.LocalDateTime

/**
 * 微盟
 */
// https://cloud.weimob.com/saas/word/detail.html?tag=1046&menuId=2
abstract class WmPorter : GenericPageDocumentPorter(), PlatformIdentity.WeiMeng {

    protected abstract val path: String

    override fun getResponse(context: JobContext, pageNo: Int, hasTotal: Boolean): JSONObject {
        val (store, startTime, endTime, parameter) = context
        val request = buildRequest(startTime, endTime, pageNo, parameter)
        val client = applicationContext.getBean(WmClient::class.java)
        val response = client.execute(store, path, request)
        if (response.getJSONObject("code").getString("errcode") != "0") {
            throwException(context, path, response)
        }
        return response.getJSONObject("data")
    }

    override fun getCount(context: JobContext, response: JSONObject): Int {
        return response.getJSONObject("data").getInteger("totalCount")
    }

    protected abstract fun buildRequest(
        startTime: LocalDateTime,
        endTime: LocalDateTime,
        pageNo: Int,
        parameter: Any?,
    ): Map<String, Any>
}

// 订单
// https://cloud.weimob.com/saas/word/detailApi.html?tag=204&menuId=1&id=344
@Component
class WmTradePorter : WmPorter() {

    override val documentType = DocumentType.TRADE

    override val path = "/order/queryOrderList"

    override fun buildDocuments(context: JobContext, response: JSONObject): List<Document> {
        return response
            .getJSONObject("data")
            .getJSONArray("pageList")
            .map {
                val value = it.toJSONObject()
                val sn = value.getString("orderNo")
                val status = value.getString("status")
                val modified = (value.getLong("paymentTime") ?: value.getLong("createTime")).toLocalDateTime()
                Document(sn, status, modified, value)
            }
    }

    override fun buildRequest(
        startTime: LocalDateTime,
        endTime: LocalDateTime,
        pageNo: Int,
        parameter: Any?
    ): Map<String, Any> {
        return mapOf(
            "pageNum" to pageNo,
            "pageSize" to pageSize,
            "queryParameter" to mapOf(
                "updateStartTime" to startTime.format(dateTimeFormatter),
                "updateEndTime" to endTime.format(dateTimeFormatter),
            )
        )
    }
}

// 退单
// https://cloud.weimob.com/saas/word/detailApi.html?tag=206&menuId=1&id=378
@Component
class WmRefundPorter : WmPorter() {

    override val documentType = DocumentType.REFUND

    override val path = "/rights/searchRightsOrderList"

    override fun buildDocuments(context: JobContext, response: JSONObject): List<Document> {
        return response
            .getJSONObject("data")
            .getJSONArray("pageList")
            .map {
                val value = it.toJSONObject()
                val sn = value.getString("id")
                val rsn = value.getString("orderNo")
                val status = value.getString("status")
                val modified = (value.getLong("paymentTime") ?: value.getLong("createTime")).toLocalDateTime()
                Document(sn, rsn, status, modified, value)
            }
    }

    override fun buildRequest(
        startTime: LocalDateTime,
        endTime: LocalDateTime,
        pageNo: Int,
        parameter: Any?,
    ): Map<String, Any> {
        return mapOf(
            "pageNum" to pageNo,
            "pageSize" to pageSize,
            "updateStartTime" to startTime.format(dateTimeFormatter),
            "updateEndTime" to endTime.format(dateTimeFormatter),
        )
    }
}